#include "elresult.h"
#include <QPaintEvent>
#include <QPainter>
#include "elicon.h"
#include "eltheme.h"

const int ElResult::Icon_Size = 48;
const int ElResult::Icon_Text_Spacing = 12;
const int ElResult::Title_Sub_Spacing = 8;
const int ElResult::Text_Footer_Spacing = 12;

ElResult::ElResult(QWidget *parent)
    : ElComponent{parent}
{
    _icon = new ElIcon(":/icons/info.svg", this);
    _icon->fillBackground(ThemeColor::infoColor);
    initPalette();
}

ElResult::ElResult(const QString &title, const QString &subTitle, QWidget *parent)
    : ElResult{parent}
{
    setTitle(title);
    setSubTitle(subTitle);
}

QString ElResult::title() const
{
    return _title;
}

void ElResult::setTitle(const QString &title)
{
    if (_title == title)
        return;
    _title = title;
}

QString ElResult::subTitle() const
{
    return _sub_title;
}

void ElResult::setSubTitle(const QString &subTitle)
{
    if (_sub_title == subTitle)
        return;
    _sub_title = subTitle;
}

ElIcon *ElResult::icon() const
{
    return _icon;
}

void ElResult::setIcon(IconType type)
{
    delete _icon;
    QString iconName;
    QColor fillColor;
    switch (type) {
    case Success:
        iconName = "success";
        fillColor = ThemeColor::successColor;
        break;
    case Warning:
        iconName = "warning";
        fillColor = ThemeColor::warningColor;
        break;
    case Error:
        iconName = "error";
        fillColor = ThemeColor::dangerColor;
        break;
    default:
        iconName = "info";
        fillColor = ThemeColor::infoColor;
        break;
    }
    _icon = new ElIcon(QString(":/icons/%1.svg").arg(iconName), this);
    _icon->fillBackground(fillColor);
    _icon->resize(Icon_Size, Icon_Size);
}

void ElResult::setIcon(const QString &fileName)
{
    delete _icon;
    _icon = new ElIcon(fileName, this);
    _icon->resize(Icon_Size, Icon_Size);
    _icon->fillBackground(ThemeColor::textPrimaryColor);
}

void ElResult::setIcon(ElIcon *icon)
{
    if (_icon == icon)
        return;
    delete _icon;
    _icon = icon;
    _icon->resize(Icon_Size, Icon_Size);
}

QWidget *ElResult::extraFooter() const
{
    return _extra_footer;
}

void ElResult::setExtraFooter(QWidget *footer)
{
    if (_extra_footer == footer)
        return;
    delete _extra_footer;
    _extra_footer = footer;
    _extra_footer->setParent(this);
    _extra_footer->hide();
}

void ElResult::paintEvent(QPaintEvent *e)
{
    QPainter painter(this);
    QRect title_br, sub_title_br;
    painter.drawText(QRect(), Qt::AlignCenter, _title, &title_br);
    painter.drawText(QRect(), Qt::AlignCenter, _sub_title, &sub_title_br);
    // calc size
    auto max_w = std::max(title_br.width(), sub_title_br.width());
    max_w = std::max(max_w, Icon_Size);
    if (_extra_footer != nullptr)
        max_w = std::max(max_w, _extra_footer->width());
    auto h = Icon_Size + Icon_Text_Spacing + title_br.height() + Title_Sub_Spacing
             + sub_title_br.height()
             + (_extra_footer == nullptr ? 0 : Text_Footer_Spacing + _extra_footer->height());
    resize(max_w, h);
    // draw icon
    _icon->move((width() - Icon_Size) / 2, 0);
    _icon->show();
    // draw text
    painter.setPen(_palette.color(ElPalette::Foreground));
    auto top = Icon_Size + Icon_Text_Spacing;
    painter.drawText(QRect(0, top, width(), title_br.height()), Qt::AlignCenter, _title);
    top += title_br.height() + Title_Sub_Spacing;
    painter.drawText(QRect(0, top, width(), sub_title_br.height()), Qt::AlignCenter, _sub_title);
    // draw extra footer
    if (_extra_footer != nullptr) {
        _extra_footer->move((width() - _extra_footer->width()) / 2,
                            height() - _extra_footer->height());
        _extra_footer->show();
    }
    e->accept();
}

void ElResult::initPalette()
{
    _palette.setColor(ElPalette::Foreground, ThemeColor::textPrimaryColor);
}
